package com.gushuang.oj.judge;

import cn.hutool.json.JSONUtil;
import com.gushuang.oj.common.ErrorCode;
import com.gushuang.oj.exception.BusinessException;
import com.gushuang.oj.judge.codesandbox.CodeSandbox;
import com.gushuang.oj.judge.codesandbox.CodeSandboxFactory;
import com.gushuang.oj.judge.codesandbox.CodeSandboxProxy;
import com.gushuang.oj.judge.codesandbox.model.ExcuteCodeRequest;
import com.gushuang.oj.judge.codesandbox.model.ExcuteCodeResponse;
import com.gushuang.oj.model.dto.question.JudgeCase;
import com.gushuang.oj.model.dto.question.JudgeConfig;
import com.gushuang.oj.model.dto.question.JudgeInfo;
import com.gushuang.oj.model.entity.Question;
import com.gushuang.oj.model.entity.QuestionSubmit;
import com.gushuang.oj.model.enums.JudgeInfoMessageEnum;
import com.gushuang.oj.model.enums.QuestionSubmitStatusEnum;
import com.gushuang.oj.service.QuestionService;
import com.gushuang.oj.service.QuestionSubmitService;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 孤霜
 * @version 1.0
 */
public class JudgeServiceImpl implements JudgeService {

    @Resource
    private QuestionService questionService;

    @Resource
    private QuestionSubmitService questionSubmitService;


    @Value("${codesandbox.type}")
    private String type;

    @Override
    public ExcuteCodeResponse doJudge(long questionSubmitId) {
        // 1) 传入题目的提交 id ，获取到对应的信息，提交信息 (包含，代码编程语言)
        QuestionSubmit questionSubmit = questionSubmitService.getById(questionSubmitId);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "提交信息不存在");
        }
        Long questionId = questionSubmit.getQuestionId();
        Question question = questionService.getById(questionId);
        if (question == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "题目不存在");
        }
        if (questionSubmit.getStatus().equals(QuestionSubmitStatusEnum.WAITING.getValue())) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "题目正在判题");
        }

        QuestionSubmit questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.RUNNING.getValue());
        boolean update = questionSubmitService.updateById(questionSubmitUpdate);
        if (!update) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "题目状态更新错误");
        }
        // 4) 调用沙箱，获取执行结果
        CodeSandbox codeSandbox = CodeSandboxFactory.newInstance(type);
        codeSandbox = new CodeSandboxProxy(codeSandbox);
        String language = questionSubmit.getLanguage();
        String code = questionSubmit.getCode();
        String judgeCaseStr = question.getJudgeCase();
        List<JudgeCase> judgeCaseList = JSONUtil.toList(judgeCaseStr, JudgeCase.class);
        List<String> inputList = judgeCaseList.stream().map(JudgeCase::getInput).collect(Collectors.toList());
        ExcuteCodeRequest excuteCodeRequest = ExcuteCodeRequest.builder().code(code).language(language).inputList(inputList).build();
        ExcuteCodeResponse excuteCodeResponse = codeSandbox.executeCode(excuteCodeRequest);
        List<String> outputList = excuteCodeResponse.getOutputList();


        return null;
    }
}
